/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.api.entityinterface;

import com.inspur.edp.bff.designtime.generator.api.VmApiGenContext;
import com.inspur.edp.bff.designtime.generator.api.entityinterface.resourceinfo.BffEntityResourceInfoGenerator;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.api.dataType.base.ICefDataType;
import com.inspur.edp.cef.api.dataType.entity.ICefEntity;
import com.inspur.edp.cef.api.dataType.entity.ICefRootEntity;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.dataTypeInfo.DataTypeInfoGenerator;
import com.inspur.edp.cef.generator.apiGenerator.dataType.entityInterface.resourceInfo.EntityResourceInfoGenerator;
import com.inspur.edp.cef.generator.apiGenerator.entity.allInterface.EntityAllInterfaceGenerator;
import com.inspur.edp.cef.generator.apiGenerator.entity.separateInterface.EntitySingleInterfaceGenerator;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import com.inspur.edp.formserver.viewmodel.GspViewObject;
import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class VoEntitySingleInterfaceGenerator extends EntitySingleInterfaceGenerator {

  private static final String NameSuffix = "VoEntity";
  private VmApiGenContext apiGenContext;
  private IGspCommonObject commonObject;

  public VoEntitySingleInterfaceGenerator(VmApiGenContext apiGenContext,
      IGspCommonObject commonObject) {
    super(apiGenContext, commonObject);
    this.apiGenContext = apiGenContext;
    this.commonObject = commonObject;
    if(((GspViewObject)commonObject).isRootNode()) {
      apiGenContext.setRootEntityAllInterfaceType(new MediateType(apiGenContext.getPackageName(),getName()));
    }
  }

  public static String getName(IGspCommonObject node) {
    return "I" + node.getCode() + NameSuffix;
  }

  @Override
  protected ArrayList<ModifierKeyword> getAccessModifier() {
    return new ArrayList<>(Arrays.asList(ModifierKeyword.PUBLIC_KEYWORD));
  }

  @Override
  protected String getName() {
    return VoEntityAllInterfaceGenerator.getName(commonObject);
  }

  @Override
  protected EntitySingleInterfaceGenerator createChildEntityGenerator(IGspCommonObject childObject) {
    return new VoEntitySingleInterfaceGenerator(apiGenContext, childObject);
  }

  protected TypeInfo getDefaultInterfaceType() {
    if (commonObject.getParentObject() == null) {
      return new TypeInfo(ICefRootEntity.class);
    } else {
      return new TypeInfo(ICefEntity.class);
    }
  }

  @Override
  protected EntityResourceInfoGenerator createResourceInfoGenerator() {
    return new BffEntityResourceInfoGenerator(getContext(), commonObject);
  }
}
